package org.wtframework.website.web.mvc.action.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.openredhead.springx.core.server.page.IPage;
import org.openredhead.springx.core.server.service.Securitys;
import org.openredhead.springx.core.web.mvc.support.ActionValueAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wtframework.platform.server.model.impl.User;
import org.wtframework.website.server.manager.InternalEmailManager;
import org.wtframework.website.server.manager.LinkManManager;
import org.wtframework.website.server.model.impl.InternalEmail;
import org.wtframework.website.server.service.InternalEmailService;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("/back/website")
public class InternalEmailActionController {
	
	private static String BASE_URL = "admin/back_layui/website/";
	
	@Autowired
	private InternalEmailManager internalEmailManager;
	
	@Autowired
	private InternalEmailService internalEmailService;
	
	@Autowired
	private LinkManManager linkManManager;
	
	
	@RequestMapping("/internalEmailManager")
	public String emailManagerUI(ModelMap modelMap, ActionValueAssistant actionValueAssistant){
		Integer currentPage=actionValueAssistant.get(Integer.class, "page");
		if(currentPage==null){
			currentPage=1;
		}
		String title=actionValueAssistant.getString("name");
		modelMap.put("internalEmailPage", internalEmailService.findInternalEmail(title,currentPage,10));
		modelMap.put("title", title);
		actionValueAssistant.addToModelMap(modelMap);
		return BASE_URL+"internalEmail/internalEmailManager";
	}
	
	@RequestMapping("/internalEmailData")
	@ResponseBody
	public Map<String, Object> noticeManager(ModelMap modelMap,ActionValueAssistant actionValueAssistant)throws ParseException{
		Map<String,Object> resultMap = Maps.newHashMap();
		String title = actionValueAssistant.get(String.class, "title");
		Integer pageNo = actionValueAssistant.get(Integer.class,"page");
		Integer pageSize = actionValueAssistant.get(Integer.class, "limit");//layui默认pageSize
//		pageSize=5;
		if(pageNo==null){
			pageNo=1;
		}	
		IPage<InternalEmail> page = internalEmailService.findInternalEmail(title, pageNo, pageSize);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		List<Map<String,Object>> list = Lists.newArrayList();
		for (InternalEmail internalEmail : page.getData()) {
			Map<String,Object> map = Maps.newHashMap();
			map.put("id", internalEmail.getId());
			map.put("title", internalEmail.getTitle());
			map.put("addressee", internalEmailService.findLinkManNameById(internalEmail.getId()));
			map.put("submiTime", sdf.format(internalEmail.getSubmiTime()));
			if(internalEmail.getPusTime()!=null)
			map.put("pusTime",sdf.format(internalEmail.getPusTime()));
			map.put("issueStatus", internalEmail.getIssueStatus());
			list.add(map);
		}
		resultMap.put("count", page.getTotalCount());
		resultMap.put("code", 0);
		resultMap.put("data", list);
		resultMap.put("msg", "");//当数据出错是输出该句子
		return resultMap;
	}
	
	
	
	@RequestMapping("/emailContent")
	public String noticeAddOrUpdate(ModelMap modelMap, ActionValueAssistant actionValueAssistant){
		User userer= Securitys.getAuthenticationUser().getUser();
		Integer emailId=actionValueAssistant.get(Integer.class,"emailId");
		if(emailId!=null){
			InternalEmail internalEmail=internalEmailManager.load(emailId);
			modelMap.put("internalEmail", internalEmail);
			modelMap.put("addresseePage", internalEmailService.findLinkManByAddressee(internalEmail.getAddressee()));
		}
		System.out.println("1111111111111111111111111111111111111");
		modelMap.put("linkManPage", linkManManager.findByQuery("from LinkMan where userId=? ", userer.getId()).getPage());
		return BASE_URL+"internalEmail/internalEmailContent";
	}
	
	@RequestMapping("/emailDetail")
	public String emailDetail( ModelMap modelMap, ActionValueAssistant actionValueAssistant){
		Integer emailId=actionValueAssistant.get(Integer.class,"emailId");
		modelMap.put("internalEmail", internalEmailManager.load(emailId));
		actionValueAssistant.addToModelMap(modelMap);
		return BASE_URL+"internalEmail/emailDetail";
	}
	
}
